package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.Adapter.HeroManitoAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.HeroManitoInfo;
import liumengbaio.lolzhangyoubao.Activity.View.xListView.XListView;
import liumengbaio.lolzhangyoubao.Activity.utils.HttpUtils;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/13.
 */
public class HeroManitoActivity extends AppCompatActivity implements XListView.IXListViewListener{
    private XListView xManitoListView;
    private HeroManitoAdapter heroManitoAdapter;
    private Button btn_heromanito;
    String id;
    /**
     * 真正加载到adapter的集合
     */
    private List<HeroManitoInfo.DataBean> infos = new ArrayList<>();

    /**
     * 中间暂存数据集合
     */
    private List<HeroManitoInfo.DataBean> temp = new ArrayList<>();

    /**
     * 页码
     */
    private int page = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heromanito);
        initView();
        initData();
        getDatafromWeb();
    }


    private void initView() {
        xManitoListView= (XListView) findViewById(R.id.manitoListview);
        btn_heromanito= (Button) findViewById(R.id.btn_heromanito_return);
        xManitoListView.setPullLoadEnable(true);
        xManitoListView.setPullRefreshEnable(true);
        xManitoListView.setXListViewListener(this);
    }

    private void initData() {
      id=getIntent().getStringExtra("userid");
        btn_heromanito.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getDatafromWeb() {
        String manitoUrl ="http://lol.service.zhangyoubao.com/service/rest/rank.getherogodlist";
        String manitoBodyUrl="params%5BpageSize%5D=100&secretSignature=HGqBvZ19%2By1l50fw4C%2Biv5kfQK4%3D&game=lol&os" +
                "=Android&secretVersion=v1.0&params%5BlastId%5D="+page+"&params%5BchampionId%5D="+id+"&sign=&secretId=" +
                "AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&params%5Bversion%5D=0&deviceId=5Xot%2FXAKrRDYLof0HC98jNX5%2B4U6gHNT%2BaraufYsX04%3D&nonce=801691&" +
                "platform=Android&apiVersion=v1&osVersion=23&platformVersion=400902&api=rank.getherogodlist&time=1473766029";
        try {
            HttpUtils.doPostAsyn(manitoUrl, manitoBodyUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
               Gson gson =new Gson();
                    HeroManitoInfo info=gson.fromJson(result,HeroManitoInfo.class);
                    if (info.getCode() == 200 && info.getData().size() == 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        temp = info.getData();
//                        if (temp.size() > 0) {
//                            handler.sendEmptyMessage(0);
//                        } else {
//                            handler.sendEmptyMessage(1);
//                        }
                    }
                }
            },handler);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    if (page == 0) {
                        //下拉刷新
                        infos.clear();
                        infos.addAll(temp);
                        heroManitoAdapter = new HeroManitoAdapter(infos,HeroManitoActivity.this);
                        xManitoListView.setAdapter(heroManitoAdapter);
                        temp.clear();
                        xManitoListView.setPullLoadEnable(true);
                    } else {
                        //上拉加载
                        infos.addAll(temp);
                        heroManitoAdapter.notifyDataSetChanged();
                        temp.clear();
                    }

                    xManitoListView.complete();
                    break;
                case 1:
                    xManitoListView.complete();
                    Toast.makeText(HeroManitoActivity.this, "网络请求失败!", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    //最后一页
                    xManitoListView.complete();
                    xManitoListView.setPullLoadEnable(false);
                    Toast.makeText(HeroManitoActivity.this, " 已经是最后一页啦 !", Toast.LENGTH_SHORT).show();
                    break;

            }
        }
    };

    @Override
    public void onRefresh() {
        //改变请求地址
        page = 0;
        //调用请求方法
        getDatafromWeb();
    }

    @Override
    public void onLoadMore() {
        //改变请求地址
        page+=100;
        //调用请求方法
        getDatafromWeb();
    }
}
